//
//  WNHomeOriginalView.swift
//  XLWeibo
//
//  Created by 风不会停息 on 2017/7/27.
//  Copyright © 2017年 风不会停息. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WNHomeOriginalView: UIView {
    
    //  记录原创为微博底部约束
    var originalViewBottomConstraint: Constraint?

    
    var statusViewModel : WNStatusViewModel?{
        
        didSet{
            
            if let imageUrl = statusViewModel?.status?.user?.profile_image_url{
                
                userImageView.sd_setImage(with: URL(string:imageUrl), placeholderImage: UIImage(named:"avatar_default_big"))

                screenNameLabel.text = statusViewModel?.status?.user?.screen_name
                contentLabel.text = statusViewModel?.status?.text
                mbrankImageView.image = statusViewModel?.mbrankImage
                verifiedTypeImageView.image = statusViewModel?.verifiedTypeImage
                sourceLabel.text = statusViewModel?.sourceContent
                timeLabel.text = statusViewModel?.timeContent
                
                if let picUrls = statusViewModel?.status?.pic_urls , picUrls.count > 0{
                    
                    pictureView.isHidden = false
                    
                    self.snp.makeConstraints({ (make) in
                        
                        originalViewBottomConstraint = make.bottom.equalTo(pictureView.snp.bottom).offset(WNCellMargin).constraint
                    })
                }
                else{
                    
                    //  1. 隐藏配图
                    pictureView.isHidden = true
                    //  2. 重新创建原创微博底部约束
                    self.snp.makeConstraints({ (make) in
                        originalViewBottomConstraint = make.bottom.equalTo(contentLabel.snp.bottom).offset(WNCellMargin).constraint
                    })

                }
            }
            
        }
    }
    
    //  MARK: --懒加载控件
    //  用户头像
    fileprivate lazy var userImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //  认证类型图片
    fileprivate lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //  昵称
    fileprivate lazy var screenNameLabel: UILabel = {
//        let label = UILabel(fontSize: 14, textColor: UIColor.darkGray)
        let label = UILabel()
        label.text = "趣闻搞笑"
        return label
    }()
    
    //  时间
    fileprivate lazy var timeLabel: UILabel = {
        let label = UILabel(fontSize: 12, textColor: UIColor.orange)
        label.text = "刚刚"
        
        return label
    }()
    
    //  来源
    fileprivate lazy var sourceLabel: UILabel = {
        let label = UILabel(fontSize: 12, textColor: UIColor.lightGray)
        label.text = "来自: 星星的你"
        
        return label
    }()
    
    //  会员等级图片
    fileprivate lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    //  微博内容
    fileprivate lazy var contentLabel: UILabel = {
        let label = UILabel(fontSize: 14, textColor: UIColor.darkGray)
        label.text = "同行的老外在深圳买了一把大弹簧刀放行李箱在普通安检的时候被查到，保安队长也是一脸嫌弃，“外国人携带管制刀具交给警察手续也很繁琐的！算你自己丢弃吧！”我说如果是我的就简单了呗，直接带走拘留"
        
        label.numberOfLines = 0
        
        return label
    }()

    //  配图
    fileprivate lazy var pictureView: WNHomePictureView = {
        let collectionView = WNHomePictureView()
        //  配图颜色和转发微博颜色一样
        collectionView.backgroundColor = self.backgroundColor
        return collectionView
    }()


    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI(){
        
        backgroundColor = UIColor.white
        
        addSubview(userImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(mbrankImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        userImageView.snp.makeConstraints { (make) in
            
            make.top.leading.equalTo(self).offset(WNCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35)).priority(.high)
        }
        
        verifiedTypeImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(userImageView.snp.trailing)
            make.centerY.equalTo(userImageView.snp.bottom)
        }
        
        screenNameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userImageView)
            make.leading.equalTo(userImageView.snp.trailing).offset(WNCellMargin)
        }
        
        timeLabel.snp.makeConstraints { (make) in
            make.leading.equalTo(screenNameLabel)
            make.bottom.equalTo(userImageView)
        }
        sourceLabel.snp.makeConstraints { (make) in
            make.leading.equalTo(timeLabel.snp.trailing).offset(WNCellMargin)
            make.bottom.equalTo(timeLabel)
        }
        mbrankImageView.snp.makeConstraints { (make) in
            make.top.equalTo(screenNameLabel)
            make.leading.equalTo(screenNameLabel.snp.trailing).offset(WNCellMargin)
        }
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userImageView.snp.bottom).offset(WNCellMargin)
            make.leading.equalTo(userImageView)
            make.trailing.equalTo(self).offset(-WNCellMargin)
        }
        
        pictureView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom).offset(WNCellMargin)
            make.leading.equalTo(contentLabel)
            //  设置默认大小，后续根据图片张数来确定真是的配图大小
            make.size.equalTo(CGSize(width: 100, height: 100)).priority(.high)
        }
        
        //(
        //    "<SnapKit.LayoutConstraint:0x6000000af420@WNHomeOriginalView.swift#166 XLWeibo.WNHomePictureView:0x7ff43687e600.top == UILabel:0x7ff435d48bb0.bottom + 10.0>",
        //    "<SnapKit.LayoutConstraint:0x6000000af5a0@WNHomeOriginalView.swift#174 XLWeibo.WNHomeOriginalView:0x7ff435d4c420.bottom == UILabel:0x7ff435d48bb0.bottom + 10.0>",
        //    "<SnapKit.LayoutConstraint:0x6000000b2ba0@WNHomeOriginalView.swift#38 XLWeibo.WNHomeOriginalView:0x7ff435d4c420.bottom == XLWeibo.WNHomePictureView:0x7ff43687e600.bottom + 10.0>"
        //)
        //
        //Will attempt to recover by breaking constraint
        //<SnapKit.LayoutConstraint:0x6000000af420@WNHomeOriginalView.swift#166 XLWeibo.WNHomePictureView:0x7ff43687e600.top == UILabel:0x7ff435d48bb0.bottom + 10.0>


        
        self.snp.makeConstraints { (make) in
            originalViewBottomConstraint =  make.bottom.equalTo(contentLabel).offset(WNCellMargin).constraint
        }
        
        
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
}
